package com.reggie.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.reggie.dto.SetmealDto;
import com.reggie.mapper.SetmealMapper;
import com.reggie.pojo.Setmeal;
import com.reggie.service.SetmealService;
import com.reggie.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    SetmealMapper setmealMapper;

    @Override
    public PageUtil<Setmeal> findAll(Integer page, Integer pageSize, String name) {
        PageUtil<Setmeal> setmealPageUtil = new PageUtil();
        PageHelper.startPage(page, pageSize);
        List<Setmeal> setmealList = setmealMapper.findAll(page, pageSize, name);
        if (setmealList==null) return null;
        Page setmealPage = (Page) setmealList;
        setmealPageUtil.setTotal(setmealPage.getTotal());
        setmealPageUtil.setRecords(setmealPage.getResult());
        return setmealPageUtil;
    }

    @Override
    public boolean delete(Long id, Long userId) {
        if (setmealMapper.delete(id, userId)==1) return true;
        return false;
    }

    @Override
    public boolean editStatus(Long id, Integer status, Long userId) {
        if (setmealMapper.editStatus(id, status, userId)==1) return true;
        return false;
    }

    @Override
    public boolean add(SetmealDto setmealDto) {
        if (setmealMapper.add(setmealDto)==1) return true;
        return false;
    }

    @Override
    public SetmealDto find(Long id) {
        SetmealDto setmealDto = setmealMapper.find(id);
        return setmealDto;
    }

    @Override
    public boolean edit(SetmealDto setmealDto) {
        if (setmealMapper.edit(setmealDto)==1) return true;
        return false;
    }

    @Override
    public List<Setmeal> findByCategoryIdAndStatus(Long categoryId, Integer status) {
        List<Setmeal> setmeals = setmealMapper.findByCategoryIdAndStatus(categoryId, status);
        return setmeals;
    }
}
